<template>
  <div class="app-container">
    <!-- 销售出货检表单页面 -->
    <el-dialog v-model="addDialog"  title="销售出货检" width="50%">
        <el-card>
          <template #header>
            <div class="card-header flex justify-between">
              <span>产品信息</span>
            </div>
          </template>
          <el-table :data="tableData" border style="width: 100%;margin: 0 auto;">
            <el-table-column label="产品编号" prop="goodsNo" align="center" width="200"/>
            <el-table-column label="产品名称" prop="goodsName" align="center" width="100"/>
            <el-table-column label="产品数量" prop="goodsCount"  align="center" width="100" />
            <el-table-column label="质检比例" prop="goodsCheckRatio" align="center" width="100"/>
            <el-table-column label="合格数" align="center" width="100">
              <template #default="scope">
                <el-input
                    v-model="scope.row.acceptance"
                    type="number"
                />
              </template>
            </el-table-column>
            <el-table-column label="不合格数" prop="disqualification" align="center" width="100">
              <template #default="scope">
                <el-input
                    v-model="scope.row.disqualification"
                    type="number"
                />
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="description" align="center" width="100">
              <template #default="scope">
                <el-input
                    v-model="scope.row.description"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      <div style="text-align:right;margin-top:20px">
        <el-button type="primary" @click="submit" :disabled="disable">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {addQuality, salesList, updateStatus} from "@/api/quality/quality.js";
import useUserStore from "@/store/modules/user.js";
import {ElMessage} from "element-plus";
import {updateOrder} from "@/api/sale/order.js";

let addDialog = defineModel('addDialog')
let outId = defineProps(["warehouseOutId","goodsType"])
const emit = defineEmits(['loadTableData'])
const input = ref()

const { proxy } = getCurrentInstance();
const { sale_order_status } = proxy.useDict("sale_order_status");

const form = ref({
  warehouseOutId: outId.warehouseOutId,
  goodsType:3,
  list:null
})

//获取登陆人id及姓名
let userId = useUserStore().id;
let userName = useUserStore().name;
// 设置查询参数中的 applyUserId 和 examineUserId
form.value.userId = userId;

let tableData = ref([])
let disable = ref(false);

// 加载数据
function getList() {
  //console.log(warehouseOutId.warehouseOutId)
  salesList({outId: outId.warehouseOutId,goodsType:3}).then((response) => {
    console.log(response)
    tableData.value = response.rows
    if (tableData.value != null && tableData.value[0].acceptance != null){
      for (let i = 0; i < tableData.value.length; i++) {
        tableData.value[i].goodsCount = tableData.value[i].acceptance+tableData.value[i].disqualification
      }
      disable.value = true;
    }
  })
}

/** 保存*/
function submit(){
  form.value.list = tableData.value
  addQuality(form.value).then((response) => {
    ElMessage({
      message: '添加成功',
      type: 'success',
      onClose: function () {
        addDialog.value = false
        getList()
      }
    })
  })
  // 修改销售出货检状态
  updateStatus({
    warehouseOutStatus:1,
    id: outId.warehouseOutId
  }).then(response => {
    getList()
  })
}

// 取消按钮
function cancel() {
  addDialog.value = false;
  getList();
}

getList()
</script>

<style scoped lang="scss">
</style>

